package com.alex.ip.holder.server;

import java.net.InetAddress;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * @author Alex.Liu
 * @date: 2025-02-18 15:12
 * @since 1.0.0
 */
@Slf4j
@SpringBootApplication
public class AlexIpHolderServerApp implements ApplicationListener<WebServerInitializedEvent> {


    public static void main(String[] args) {
        ConfigurableApplicationContext app = SpringApplication.run(AlexIpHolderServerApp.class, args);
    }

    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        try {
            String host = InetAddress.getLocalHost().getHostAddress();
            int port = event.getWebServer().getPort();
            String contextPath = event.getApplicationContext().getEnvironment().getProperty("server.servlet.context-path");
            log.info("Starting AlexIpHolder Successfully Local   :  http://localhost:{}{}", port, contextPath);
            log.info("Starting AlexIpHolder Successfully External:  http://{}:{}{}", host, port, contextPath);
        } catch (Exception e) {
            // do nothing
        }
    }
}
